package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.bringway.framework.dao.P2PJdbcDao;
import com.bringway.p2p.dao.ModSjhmDao;

import net.sf.json.JSONObject;
@Repository
public class ModSjhmDaoImpl extends P2PJdbcDao implements ModSjhmDao {
	
	private Logger logger=Logger.getLogger(ModSjhmDaoImpl.class);

	@Override
	public List<Map<String, Object>> queryModSjhmListAll(JSONObject params) {
		int start = params.optInt("start");
		int limit = params.optInt("limit");
		String startDate=params.optString("startDate");
		String endDate=params.optString("endDate");
		String yhm=params.optString("yhm");
		String sjhmy=params.optString("sjhmy");
		String sjhmx=params.optString("sjhmx");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append("select ID, USER_NAME, SJHM_Y, SJHM_X, MOD_TABLE_NAME, MOD_COLUMN_NAME,date_format(SJHM_CHANGE_DATE,'%Y-%m-%d %H:%i:%s') SJHM_CHANGE_DATE from t_zhfw_sjhm_mod_log");
		sql.append(" where 1=1");
		if (StringUtils.isNotEmpty(startDate)) {
			sql.append(" and date_format(SJHM_CHANGE_DATE,'%Y-%m-%d') >=:startDate");
			paramMap.put("startDate", startDate);
		}
		if (StringUtils.isNotEmpty(endDate)) {
			sql.append(" and date_format(SJHM_CHANGE_DATE,'%Y-%m-%d') <=:endDate");
			paramMap.put("endDate", endDate);
		}
		if (StringUtils.isNotEmpty(yhm)) {
			sql.append(" and USER_NAME like :yhm");
			paramMap.put("yhm", "%"+yhm+"%");
		}
		if (StringUtils.isNotEmpty(sjhmy)) {
			sql.append(" and SJHM_Y like :sjhmy");
			paramMap.put("sjhmy", "%"+sjhmy+"%");
		}
		if (StringUtils.isNotEmpty(sjhmx)) {
			sql.append(" and SJHM_X like :sjhmx");
			paramMap.put("sjhmx", "%"+sjhmx+"%");
		}
		sql.append(" order by SJHM_CHANGE_DATE desc");
		if (limit>0) {
			sql.append(" limit :start,:limit");
			paramMap.put("start", start);
			paramMap.put("limit", limit);
		}
		logger.info("queryModSjhmListAll sql is "+sql.toString());
		logger.info("queryModSjhmListAll paramMap is "+paramMap);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}

	@SuppressWarnings("deprecation")
	@Override
	public int getModSjhmCount(JSONObject params) {
		String startDate=params.optString("startDate");
		String endDate=params.optString("endDate");
		String yhm=params.optString("yhm");
		String sjhmy=params.optString("sjhmy");
		String sjhmx=params.optString("sjhmx");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		sql.append("select count(*) from t_zhfw_sjhm_mod_log where 1=1");
		if (StringUtils.isNotEmpty(startDate)) {
			sql.append(" and date_format(SJHM_CHANGE_DATE,'%Y-%m-%d') >=:startDate");
			paramMap.put("startDate", startDate);
		}
		if (StringUtils.isNotEmpty(endDate)) {
			sql.append(" and date_format(SJHM_CHANGE_DATE,'%Y-%m-%d') <=:endDate");
			paramMap.put("endDate", endDate);
		}
		if (StringUtils.isNotEmpty(yhm)) {
			sql.append(" and USER_NAME like :yhm");
			paramMap.put("yhm", "%"+yhm+"%");
		}
		if (StringUtils.isNotEmpty(sjhmy)) {
			sql.append(" and SJHM_Y like :sjhmy");
			paramMap.put("sjhmy", "%"+sjhmy+"%");
		}
		if (StringUtils.isNotEmpty(sjhmx)) {
			sql.append(" and SJHM_X like :sjhmx");
			paramMap.put("sjhmx", "%"+sjhmx+"%");
		}
		logger.info("getModSjhmCount sql is "+sql.toString());
		logger.info("getModSjhmCount paramMap is "+paramMap);
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}

}
